package com.cyh.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 直接缓冲区，加快I/O速度
 * */
public class BufferDemo05 {

    public static void main(String[] args) throws Exception {
       String inFile = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\4.txt";
        FileInputStream fin = new FileInputStream(inFile);
        FileChannel finChannel = fin.getChannel();

        String outFile = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\5.txt";
        FileOutputStream fout = new FileOutputStream(outFile);
        FileChannel foutChannel = fout.getChannel();

        //创建直接缓冲区
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        while(true) {
            buffer.clear();

            int r = finChannel.read(buffer);
            if(r == -1) {
                break;
            }
            buffer.flip();
            foutChannel.write(buffer);
        }
    }
}
